package com.bibrApp.difu.actvity.security_setting_mvp;


import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.widget.TextView;

import com.bibrApp.difu.DfApp;
import com.bibrApp.difu.R;
import com.bibrApp.difu.Response.LoginLogResBody;
import com.bibrApp.difu.Response.LoginResponseBody;
import com.bibrApp.difu.actvity.certificate_mvp.CertificateActivity;
import com.bibrApp.difu.actvity.fund_security_code_mvp.FundSecurityCodeActivity;
import com.bibrApp.difu.actvity.reset_pwd_mvp.ReSetPwdActivity;
import com.bibrApp.difu.adapter.LoginLogAdapter;
import com.bibrApp.difu.base.BaseActivity;
import com.bibrApp.difu.utils.IntentUtil;
import com.bibrApp.difu.utils.ToastUtils;
import com.bibrApp.difu.widget.HorizontalDividerItemDecoration;
import com.gyf.barlibrary.ImmersionBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

public class SecuritySettingActivity extends BaseActivity<SecuritySettingPresenter> implements ISecuritySettingView {

    private ImmersionBar mImmersionBar;
    private Context mContext;
    @BindView(R.id.fund_security_code_tv)
    TextView fund_security_code_tv;
    @BindView(R.id.certificate_tv)
    TextView certificate_tv;
    @BindView(R.id.login_pwd_tv)
    TextView login_pwd_tv;
    @BindView(R.id.smart_refresh_layout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    LoginLogAdapter adapter;
    private DfApp dfApp;
    @Override
    protected void initPresenter() {
        presenter = new SecuritySettingPresenter(this, this);
        presenter.init();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_security_setting;
    }

    @Override
    public void initView() {
        mContext = this;
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.statusBarColor(R.color.white)
                .statusBarDarkFont(true)
                .fitsSystemWindows(true)
                .init();

        dfApp = (DfApp) getApplication();
        LoginResponseBody loginResponseBody = dfApp.getmData();
        if (loginResponseBody != null) {
            if (!TextUtils.isEmpty(loginResponseBody.getCenterUserMain().getTradePassword())) {
                fund_security_code_tv.setText("已设置");
            } else {
                fund_security_code_tv.setText("未设置");
            }
            login_pwd_tv.setText("已设置");
            switch (loginResponseBody.getCenterUserMain().getSeniorCertification()) {
                case 1:
                    certificate_tv.setText("认证中");
                    break;
                case 2:
                    certificate_tv.setText("未通过");
                    break;
                case 3:
                    certificate_tv.setText("已认证");
                    break;
                default:
                    certificate_tv.setText("未认证");
                    break;
            }

        }
        adapter = new LoginLogAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(mContext)
                .color(getResources().getColor(R.color.black_f0))
                .build());
        recyclerView.setAdapter(adapter);

        smartRefreshLayout.autoRefresh();
        smartRefreshLayout.setOnRefreshListener(refreshLayout -> {
            mPageNum = 1;
            loadData();
        });
        smartRefreshLayout.setOnLoadMoreListener(refreshLayout -> {
            mPageNum++;
            loadData();
        });

    }

    private static final int PAGE_SIZE = 10;
    private int mPageNum = 1;

    private void loadData() {
        presenter.queryLoginLog(PAGE_SIZE, mPageNum ,dfApp.getUserId());
    }


    @Override
    public void error(String msg) {
        ToastUtils.ShowToast(mContext, msg);
    }

    @Override
    public void success(String msg) {

    }

    private List<LoginLogResBody.loginItem> loginLogResBodies = new ArrayList<>();

    @Override
    public void successOfLoginLog(List<LoginLogResBody.loginItem> datas ,boolean hasNextPage) {
        if (datas == null || datas.size() == 0) {
            return;
        }
        if (mPageNum == 1) {
            loginLogResBodies.clear();
            smartRefreshLayout.finishRefresh();
        }
        if(hasNextPage){
            smartRefreshLayout.setEnableLoadMore(true);
            loginLogResBodies.addAll(datas);
            smartRefreshLayout.finishLoadMore();
        }else{
            smartRefreshLayout.setEnableLoadMore(false);
            loginLogResBodies.addAll(datas);
            smartRefreshLayout.finishLoadMore();
        }
        adapter.setNewData(loginLogResBodies);
    }

    @OnClick(R.id.return_iv)
    public void onClickBack() {
        finish();
    }

    @OnClick(R.id.fund_security_code_rl)
    public void onClickFundSecurityCode() {
        IntentUtil.toActivity(FundSecurityCodeActivity.class, this);
    }

    @OnClick(R.id.certificate_rl)
    public void onClickCertificate() {
        IntentUtil.toActivity(CertificateActivity.class, this);
    }

    @OnClick(R.id.login_pwd_rl)
    public void onClickResetPwd() {
        IntentUtil.toActivity(ReSetPwdActivity.class, this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mImmersionBar != null) {
            mImmersionBar.destroy();
        }

    }

}
